{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 比较递归和循环的计算时间【容易】"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "斐波那契数列满足：\n",
    "\n",
    "fib(1)=fib(2)=1\n",
    "\n",
    "fib(n) = fib(n-1)+fib(n-2) \n",
    "\n",
    "下面为两种计算斐波那契数列的方法。比较它们的计算时间，哪种方法更快一些？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "832040\n",
      "832040\n"
     ]
    }
   ],
   "source": [
    "# 求斐波那契数列第30个元素\n",
    "\n",
    "### 方法A:利用循环\n",
    "\n",
    "def fib(n):\n",
    "    if n in {1,2}:\n",
    "        return(1)\n",
    "    i = 2\n",
    "    a,b = 1,1\n",
    "    while i<n:\n",
    "        i+=1\n",
    "        a,b=b,a+b\n",
    "    return(b)\n",
    "print(fib(30))\n",
    "\n",
    "### 方法B:利用递归\n",
    "\n",
    "def fib(n):\n",
    "    return(1 if n in (1,2) else fib(n-1)+fib(n-2))\n",
    "print(fib(30))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#==============================答题区==============================#\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "#==============================答题区==============================#"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#==============================提示区==============================#\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "#  提示：递归的实现原理\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "#==============================提示区==============================#"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 9 µs, sys: 0 ns, total: 9 µs\n",
      "Wall time: 12.6 µs\n",
      "CPU times: user 283 ms, sys: 3.47 ms, total: 287 ms\n",
      "Wall time: 289 ms\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "832040"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#==============================答案区==============================#\n",
    "\n",
    "# 求斐波那契数列第30个元素\n",
    "\n",
    "### 方法A:利用循环\n",
    "\n",
    "def fib(n):\n",
    "    if n in {1,2}:\n",
    "        return(1)\n",
    "    i = 2\n",
    "    a,b = 1,1\n",
    "    while i<n:\n",
    "        i+=1\n",
    "        a,b=b,a+b\n",
    "    return(b)\n",
    "%time fib(30)\n",
    "\n",
    "### 方法B:利用递归\n",
    "\n",
    "def fib(n):\n",
    "    return(1 if n in (1,2) else fib(n-1)+fib(n-2))\n",
    "%time fib(30)\n",
    "\n",
    "\n",
    "\n",
    "#递归的实现涉及到使用堆栈保存现场，在这个例子中会导致大量的重复计算。\n",
    "#递归虽然可以增加代码的可读性，但其效率通常比较低。\n",
    "#程序中应当避免过深的递归。\n",
    "\n",
    "#==============================答案区==============================#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](../Python与算法之美.jpg)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
